﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GRUPO_4.Negocio;
using GRUPO_4.Dominio;

namespace GRUPO_4.Controllers
{
    public class ContratoController : Controller
    {
        //
        ContratoService ContratoService { get; set; }
        // GET: /Contrato/

        public ActionResult Index()
        {
            ICollection<Contrato> Model = ContratoService.ListarTodoslosContratos();
            return View(Model);
        }

        //
        // GET: /Contrato/Details/5

        public ActionResult Details(int codContrato)
        {
            Contrato Model = ContratoService.ObtenerContrato(codContrato);  
            return View(Model);
        }

        //
        // GET: /Contrato/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Contrato/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                if (ModelState.IsValid)
                {
                    Contrato nuevoContrato = new Contrato();

                    nuevoContrato.codContrato = int.Parse((String)collection["codContrato"]);
                    nuevoContrato.descripcionContrato = (String)collection["descripcionContrato"];

                    ContratoService.CrearContrato(nuevoContrato);
           
                    return RedirectToAction("Index");
                }

                return View();
            }
            catch (Exception e)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
            {
                //ModelState.AddModelError(String.Empty, "Error interno al intentar crear el contrato");
                ModelState.AddModelError(String.Empty, e.Message);
                return View();
            }
        }
        
        //
        // GET: /Contrato/Edit/5
 
        public ActionResult Edit(int codContrato)
        {
            Contrato Model = ContratoService.ObtenerContrato(codContrato);
            return View(Model);
        }

        //
        // POST: /Contrato/Edit/5

        [HttpPost]
        public ActionResult Edit(int codContrato, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // TODO: Add update logic here
                    Contrato contratoModificar = ContratoService.ObtenerContrato(codContrato);
                    contratoModificar.codContrato = int.Parse((String)collection["codContrato"]);   
                    contratoModificar.descripcionContrato = (String)collection["descripcionContrato"];

                    ContratoService.ModificarContrato(contratoModificar);
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch (Exception e)
            {
                //ModelState.AddModelError(String.Empty, "Error interno al intentar crear el contrato");
                ModelState.AddModelError(String.Empty, e.Message);
                return View();
            }
        }

        //
        // GET: /Contrato/Delete/5
 
        public ActionResult Delete(int codContrato)
        {
            Contrato Model = ContratoService.ObtenerContrato(codContrato);
            return View(Model);
        }

        //
        // POST: /Contrato/Delete/5

        [HttpPost]
        public ActionResult Delete(int codContrato, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Contrato contratoEliminar = ContratoService.ObtenerContrato(codContrato);
                ContratoService.EliminarContrato(contratoEliminar);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
